package com.sinaapp.gavinzhang.ninth;

import org.junit.Test;

/**
 * Created by gavin on 15-8-12.
 */
public class Optimize {


    public void oderSearch()
    {
        int[] tmp = new int[100000000];
        for (int i = 0; i < tmp.length; i++) {
            tmp[i] = i;
        }

        long start = System.currentTimeMillis();
        for (int i = 0;i < tmp.length; i++) {
            if (tmp[i] == tmp.length-2){
                System.out.println("searched it");
                break;
            }
        }
        System.out.println(System.currentTimeMillis()-start);

        start = System.currentTimeMillis();
        int hold = tmp[tmp.length-1];
        tmp[tmp.length-1] = tmp.length-2;
        int j = 0;
        do{
            if (tmp[j] == tmp.length-2)
            {
                break;
            }
        }while (++j!=0);
        tmp[tmp.length-1] = hold;
        if (j != tmp.length-1){
            System.out.println("searched id");
        }
        System.out.println(System.currentTimeMillis()-start);
    }

    @Test
    public void binarySearch()
    {//可找出在最前面的符合的数
        int[] tmp = new int[100000000];
        for (int i = 0; i < tmp.length; i++) {
            tmp[i] = i;
        }
        int low = -1;
        int upper = tmp.length;
        while (low+1 != upper){
            int middle = (low+upper)/2;
            if (tmp[middle] < 500000){
                low = middle;
            }else {
                upper = middle;
            }
        }
        int position = upper;
        if (position>=tmp.length || tmp[position]!=500000){
            System.out.println("not searched");
        }else {
            System.out.println("searched it");
        }
    }
}
